<?php

namespace common\models\datas;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%card_re_send}}".
 *
 * @property integer $id
 * @property integer $number
 * @property integer $card_start
 * @property integer $card_end
 * @property integer $salesman_id
 * @property string $issuser_id
 * @property integer $batch_id
 * @property integer $created_at
 * @property integer $proportion
 */
class CardReSend extends \common\components\bases\BaseModel
{
    public $card_start_re;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%card_re_send}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['number', 'card_start', 'card_end', 'issuser_id', 'card_send_id', 'created_at', 'proportion','card_start_re'], 'integer'],
            [['salesman'], 'string'],
            [['number', 'salesman', 'proportion'], 'required','except'=>'rewithdraw'],
            [['number'], 'check_number','except'=>'rewithdraw'],
            [['proportion'], 'check_proportion','except'=>'rewithdraw'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'number' => Yii::t('common', '发卡数量'),
            'card_start' => Yii::t('common', '开始编号'),
            'card_end' => Yii::t('common', '结束编号'),
            'salesman' => Yii::t('common', '发卡人'),
            'issuser_id' => Yii::t('common', '二次发卡人'),
            'card_send_id' => Yii::t('common', '所属批次'),
            'created_at' => Yii::t('common', '创建时间'),
            'proportion' => Yii::t('common', '分成比例'),
            'card_start_re' => Yii::t('common', '收回开始卡号'),
        ];
    }


    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }

    public function getCardSend()
    {
        return $this->hasOne(CardSend::className(), ['id' => 'card_send_id']);
    }

    public function check_number($attribute)
    {
        if (!empty($this->card_send_id)) {
            $card_send = CardSend::findOne(['id' => $this->card_send_id]);
            if ($card_send === null) {
                $this->addError('card_send_id', Yii::t('wechatzht', '发卡信息不存在'));
            } else {
                $card_re_send = CardReSend::find()->where(['batch_id' => $card_send->batch_id, 'card_send_id' => $this->card_send_id])->all();
                $count = 0;
                foreach ($card_re_send as $k => $v) {
                    $count += $v->number;
                }
                $surplus = $card_send->number - $count;
                if ($this->$attribute > $surplus) {
                    $this->addError($attribute, Yii::t('wechatzht', '最多发卡数量为{count}', ['count' => $surplus]));
                }
            }
        }
    }

    public function check_proportion($attribute)
    {
        if ($this->$attribute < 0 || $this->$attribute > 100) {
            $this->addError($attribute, Yii::t('wechatzht', '比例分成必须为0-100的整数'));
        }
    }

    public function getBatch()
    {
        return $this->hasOne(CardBatch::className(), ['id' => 'batch_id']);
    }

}
